package com.zhuiyun.educations.api.live.service.impl;

import com.zhuiyun.educations.api.live.controller.LiveController;
import com.zhuiyun.educations.api.live.entity.LiveEntity;
import com.zhuiyun.educations.api.live.mapper.LiveMapper;
import com.zhuiyun.educations.api.live.service.LiveService;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.UploadFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LiveServiceImpl
 * @Description TODO
 * @Author cjl
 * @Date 2023/6/28 10:48
 **/
@Service
public class LiveServiceImpl implements LiveService {

    @Autowired
    LiveMapper liveMapper;


    @Override
    public CommonResult getLiveByUserId(Integer userId) {
        LiveEntity liveEntity = liveMapper.getLiveByUserId(userId);
        return CommonResult.ok(liveEntity);
    }

    @Override
    public CommonResult saveLive(LiveEntity liveEntity, MultipartRequest multipartRequest) {
//        MultipartFile file = multipartRequest.getFile("file");
//        Integer coverId = Integer.valueOf(UploadFileUtils.uploadFile(file));
        Integer coverId = 1384;
        String streamName = DigestUtils.md5DigestAsHex(liveEntity.getUserId().toString().getBytes());
        Map<Object, Object> pushUrlAndPlayUrl = LiveController.createPushUrlAndPlayUrl(streamName);
        // 推流地址
        Object streamKey = pushUrlAndPlayUrl.get("streamKey");
        // 播流地址
        Object playUrl = pushUrlAndPlayUrl.get("playUrl");
        // 服务器地址
        Object pushUrlHead = pushUrlAndPlayUrl.get("pushUrlHead");
        liveEntity.setLivePlayUrl(playUrl.toString());
        liveEntity.setLiveServerUrl(pushUrlHead.toString());
        liveEntity.setStreamKey(streamKey.toString());
        liveEntity.setCoverId(coverId);
        liveEntity.setLiveStatus(0);
        // 随即设置直播间房间号
        Integer roomNum = checkRommNum();
        liveEntity.setRoomNum(roomNum);
        liveEntity.setCreateTime(new Date());
        int insert = liveMapper.insert(liveEntity);
        System.out.println("liveEntity:::"+liveEntity);
        if (insert > 0) {
            return CommonResult.ok(insert);
        } else {
            return CommonResult.error("401");
        }
    }

    @Override
    public CommonResult getLiveById(Integer id) {
        LiveEntity liveById = liveMapper.getLiveById(id);
        return CommonResult.ok(liveById);
    }

    @Override
    public CommonResult checkLiveByUserId(Integer userId) {
        Integer integer = liveMapper.checkLiveByUserId(userId);
        if(integer > 0) {
            return CommonResult.error("401");
        } else {
            return CommonResult.ok(integer);
        }
    }

    @Override
    public CommonResult listLive() {
        Example example = new Example(LiveEntity.class);
        example.createCriteria().andEqualTo("liveStatus",0);
        List<LiveEntity> liveEntities = liveMapper.selectByExample(example);
        return CommonResult.ok(liveEntities);
    }

    @Override
    public CommonResult closeLive(Integer id) {
        LiveEntity liveEntity = new LiveEntity();
        liveEntity.setId(id);
        liveEntity.setLiveStatus(1);
        int i = liveMapper.updateByPrimaryKeySelective(liveEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("401");
        }
    }

    /**
    * @Author cjl
    * @Description 生成房间号
    * @Date 2023/6/28 19:59
    * @Return java.lang.Integer

    */
    public Integer checkRommNum(){
        int roomNum = (int) (((Math.random() * 9) + 1) * 100000);
        // 校验直播间 房间号是否重复
        Integer roomNumCount = liveMapper.checkRoomNum(roomNum);
        if (roomNumCount > 0) {
            checkRommNum();
        }
        return roomNum;
    }

}
